var express = require('express');
var router = express.Router();
var http = require('http');
var url = require('url');
router.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});
router.get('/figure', function(req, res, next) {
	var t = req.query.time;
	var url = 'http://m.maizuo.com/v4/api/billboard/home?__t='+t;
  	http.get(url,function (response) {

  		var data = '';
  		response.on('data',function (chunk) {
  			data += chunk; 
  		})
  		response.on('end',function () {
  			res.send(data)
  		})
  	})
});
router.get('/hot', function(req, res, next) {
	var t = req.query.time;
  var c = req.query.count;
  var p = req.query.page;
	var url = 'http://m.maizuo.com/v4/api/film/now-playing?__t='+t+'&page='+p+'&count='+c;
  	http.get(url,function (response) {

  		var data = '';
  		response.on('data',function (chunk) {
  			data += chunk; 
  		})
  		response.on('end',function () {
  			res.send(data)
  		})
  	})
});
router.get('/will', function(req, res, next) {
	var t = req.query.time;
  var c = req.query.count;
  var p = req.query.page;
	var url = 'http://m.maizuo.com/v4/api/film/coming-soon?__t='+t+'&page='+p+'&count='+c;
  	http.get(url,function (response) {

  		var data = '';
  		response.on('data',function (chunk) {
  			data += chunk; 
  		})
  		response.on('end',function () {
  			res.send(data)
  		})
  	})
});
//详情接口
router.get(/\d$/,function(req,res) {
  var t = req.query.time;
  var id = url.parse(req.url).pathname.substr(1);
  var detailUrl = "http://m.maizuo.com/v4/api/film/" +id+ "?__t=" + t;
  http.get(detailUrl,function(response) {
    var data = '';
    response.on('data',function(chunk) {
      data += chunk;
    })
    response.on('end',function() {
      res.send(data);
    })
  })     
})
//地址接口
router.get('/cinema',function(req,res) {
  var t = req.query.time;
  var url = "http://m.maizuo.com/v4/api/cinema?__t="+t;
  http.get(url,function(response) {
    var data = '';
    response.on('data',function(chunk) {
      data += chunk;
    })
    response.on('end',function() {
      res.send(data);
    })
  })
})

module.exports = router;
